﻿package mortal.game.view.investmentProject
{
    import Message.Public.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import extend.language.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class InvestmentController extends Controller
    {
        private var _module:InvestmentModule;

        public function InvestmentController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new InvestmentModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.isShowIconEffect);
            NetDispatcher.addCmdListener(ServerCommand.SystemClientSettingGot, this.isShowIconEffect);
            NetDispatcher.addCmdListener(ServerCommand.InvestmentInfoUpdate, this.updateInvestment);
            Dispatcher.addEventListener(EventName.InvestmentGet, this.investmentGet);
            Dispatcher.addEventListener(EventName.InvestmentOpen, this.openWin);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.updateInvestment);
            return;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.Investment, this.investmen);
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.Investment, this.investmen);
            return;
        }// end function

        private function investmen(event:DataEvent) : void
        {
            var type:int;
            var money:int;
            var closeAlert:Function;
            var e:* = event;
            closeAlert = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    if (Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, money))
                    {
                        GameProxy.player.investment(type);
                    }
                    else
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35108), MsgRollTipsType.msgRollTips3);
                    }
                }
                return;
            }// end function
            ;
            type = e.data as int;
            money;
            Alert.show(Language.getStringByParam(35321, money), null, Alert.OK | Alert.CANCEL, null, closeAlert, null, 4);
            return;
        }// end function

        private function investmentGet(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            GameProxy.player.getinvest(_loc_2.operType, _loc_2.type, _loc_2.day);
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            if (Global.isDebugModle || Cache.instance.role.roleInfo.level >= GameDefConfig.instance.getSystemOpenLevel(21))
            {
                if (!this.isViewShow)
                {
                    view.show();
                    if (!ClientSetting.local.getIsDone(IsDoneType.HasClickInvestment))
                    {
                        ClientSetting.local.setIsDone(true, IsDoneType.HasClickInvestment);
                        ClientSetting.save();
                        this.isShowIconEffect();
                    }
                }
                else
                {
                    view.hide();
                }
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(35000, GameDefConfig.instance.getSystemOpenLevel(21).toString()));
            }
            return;
        }// end function

        private function updateInvestment(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (param1 is int)
            {
                _loc_2 = param1 as int;
                if (_loc_2 != 99 && _loc_2 != EInvestType._EInvestTypeResigst)
                {
                    return;
                }
            }
            if (Cache.instance.role.entityInfo.level > GameDefConfig.instance.getSystemOpenLevel(21) && (ClockManager.instance.getLoginDayNumByPassZero() <= 7 || InvestUtil.isHasInvestmented(EInvestType._EInvestTypeResigst) && !InvestUtil.isHasGetAllInvest(EInvestType._EInvestTypeResigst)))
            {
                InvestmentIcon.instance.show();
                this.isShowIconEffect();
            }
            else if (InvestmentIcon.isViewShow)
            {
                InvestmentIcon.instance.hide();
            }
            if (this.isViewShow)
            {
                this._module.update();
            }
            return;
        }// end function

        private function isShowIconEffect(param1:Object = null) : void
        {
            if (Cache.instance.role.roleInfo.level >= GameDefConfig.instance.getSystemOpenLevel(21))
            {
                if (!ClientSetting.local.getIsDone(IsDoneType.HasClickInvestment))
                {
                    InvestmentIcon.instance.playEffect();
                }
                else if (InvestUtil.isHasInvestmentCanGetByType(EInvestType._EInvestTypeResigst))
                {
                    InvestmentIcon.instance.playEffect();
                }
                else
                {
                    InvestmentIcon.instance.stopEffect();
                }
                NetDispatcher.removeCmdListener(ServerCommand.RoleLevelUpdate, this.isShowIconEffect);
            }
            else
            {
                InvestmentIcon.instance.stopEffect();
            }
            return;
        }// end function

    }
}
